package com.solocredit.finance.ui.steps.adapter

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.solocredit.finance.R
import com.solocredit.finance.base.adapter.MullaRecyclerViewAdapter
import com.solocredit.finance.base.adapter.MullaViewHolderHelper
import com.solocredit.finance.base.utils.ImageUtil
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/31 12:02
 * @Annotation：绑卡适配器
 */
class MullaStepAccountAdapter(recyclerView: RecyclerView?): MullaRecyclerViewAdapter<JSONObject>(recyclerView) {

    override fun getItemViewType(position: Int): Int {
        val model = getItem(position)
        if (model.optBoolean("isEdit",false)){
            return R.layout.item_mulla_account_edit
        }
        return R.layout.item_mulla_account_index
    }

    override fun setItemChildListener(helper: MullaViewHolderHelper, viewType: Int) {
        helper.setItemChildClickListener(R.id.editLayout) //点击编辑
        helper.setItemChildClickListener(R.id.imgDelete)//删除
    }

    /**
     * 去除编辑状态的item数量
     */
    fun getAccountCount(): Int {
        data?.let {
            var itemCount = 0
            for (i in it.indices){
                val item = it[i]
                //编辑状态不算已绑卡数量
                if (item.optBoolean("isEdit",false)){
                    continue
                }
                itemCount += 1
            }
            return itemCount
        }
        return super.getItemCount()
    }

    override fun fillData(helper: MullaViewHolderHelper, position: Int, model: JSONObject) {
        helper.setText(R.id.textIndex,String.format(mContext.getString(R.string.step_bind_card_index),position+1))

        //是否是编辑
        val isEdit = model.optBoolean("isEdit",true)
        if (!isEdit){
            helper.setText(R.id.textName,model.optString("walletDesc"))
            helper.setText(R.id.textNumber,"+" + model.optString("walletMobile"))

            val imgLogo = helper.getImageView(R.id.imgLogo)
            ImageUtil.instance?.loadImageUrl(mContext,imgLogo,model.optString("walletLogo"), R.mipmap.icon_holder)

            //是否显示 删除按钮
            if (model.has("isShowDelete") && !model.optBoolean("isShowDelete")){
                helper.setVisibility(R.id.imgDelete, View.GONE)
            } else {
                helper.setVisibility(R.id.imgDelete, View.VISIBLE)
            }
        }
    }
}